package fr.utbm.aiechat.structures;

import fr.utbm.aiechat.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class UserAdapter extends ArrayAdapter<User>
{
	private Context context;
	private int textViewResourceId;
	
	public UserAdapter(Context context, int textViewResourceId)
	{
		super(context, textViewResourceId);
		this.context = context;
		this.textViewResourceId = textViewResourceId;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View row = convertView;
        UserHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(textViewResourceId, parent, false);
           
            holder = new UserHolder();
            holder.NickUser = (TextView)row.findViewById(R.id.userNick);
           
            row.setTag(holder);
        }
        else
        {
            holder = (UserHolder)row.getTag();
        }
       
        User user = getItem(position);
        holder.NickUser.setText(user.getName());
        
        if(user.getSexe().equals("Homme")){
        	holder.NickUser.setTextColor(Color.rgb(67, 103, 210));
        }
        else{
        	holder.NickUser.setTextColor(Color.rgb(224, 159, 202));
        }
        
        
        
        return row;
    }
   
    static class UserHolder
    {
        TextView NickUser;
    }
    
    public boolean userExist(User u)
    {
    	for(int i = 0; i < getCount(); i++)
    	{
    		User user = getItem(i);
    		if(u.getAddress().equals(user.getAddress()))
    		{
    			return true;
    		}
    	}
    	
    	return false;
    }
}
